﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using digitwhu.Logic.BusinessLogicControl;
using System.IO;
using digitwhu.Helper;


namespace digitwhu.Controllers
{
    public class CanteenController : Controller
    {
        //推荐列表
        public String GetRecommendList(String buildingId)
        {
            return BuildingLogicControl.GetCanRecommendList(buildingId);
        }
        //增加推荐
        //先上传图片，后上传内容
        public String AddRecommend(String userId, String buildingId, String contents,String picture)
        {
            //获得一个唯一的文件名
            String unix = CanteenHelper.ConvertDateTimeInt(DateTime.Now).ToString();
            String id = unix + CanteenHelper.GenerateRandomNumber(4);
            picture = this.savePicture(id);
            return BuildingLogicControl.AddCanRecommendFood(userId, buildingId, contents, picture);
        }
        //内容上传
        public String UpdateRecommend(String recommendId,String contents,String picture)
        {
            return BuildingLogicControl.UpdateCanRecommendFood(recommendId, contents, picture);
        }
        //留言列表
        public String GetMsgList(String buildingId)
        {
            return BuildingLogicControl.GetCanMessageList(buildingId);
        }
        //增加留言
        public String AddMsg(String userId,String buildingId,String content)
        {
            return BuildingLogicControl.AddCanMessage(userId, buildingId, content);
        }

        //code from stackoverflow.com
        //http://stackoverflow.com/questions/5272619/how-do-i-perform-a-ajax-jquery-upload-of-an-image-in-asp-net-mvc
        //using fileuploader.js at https://github.com/valums/file-uploader
        private String savePicture(String id)
        {
            bool IsIE = false;
            string sFileName = "";
            //保存目录名
            var TempFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "static/images");
            //是否上传文件
            if ((Request.Files == null) || (Request.Files.Count == 0))
            {
                if (string.IsNullOrEmpty(Request.Params["qqfile"]))
                {
                    return ("{success:false, error:'request file is empty'}");
                }
                else
                {
                    sFileName = Request.Params["qqfile"].ToString();
                }
            }
            else
            {
                sFileName = Request.Files[0].FileName;
                IsIE = true;
            }
            //上传文件是否为空
            if (string.IsNullOrEmpty(sFileName))
            {
                return ("{success:false, error:'request file is empty'}");
            }

            string DocumentName = id + Path.GetExtension(sFileName);
            //IE浏览器特殊处理代码
            if (IsIE)
            {
                try
                {
                    Request.Files[0].SaveAs(Path.Combine(TempFolder, DocumentName));
                }
                catch (Exception ex)
                {
                    return ("{success:false, error:'" + ex.Message + "'}");
                }
            }
            else
            {
                try
                {
                    if ((Request.InputStream != null) && (Request.InputStream.CanRead) && (Request.InputStream.Length > 0))
                    {
                        using (FileStream fileStream = new FileStream(Path.Combine(TempFolder, DocumentName), FileMode.Create))
                        {
                            byte[] FileBytes = new byte[Convert.ToInt32(Request.InputStream.Length) + 1];
                            Int32 bytesRead = 0;
                            bytesRead = Request.InputStream.Read(FileBytes, 0, FileBytes.Length);
                            fileStream.Write(FileBytes, 0, bytesRead);
                            fileStream.Flush();
                            fileStream.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return ("{success:false, error:'" + ex.Message + "'}");
                }
            }

            return DocumentName;
        }

    }
}
